package com.xmad.ksadutils.AdSDK.AdMold.Pangolin;


import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;

import java.util.List;

public class PGloadInteractionExpressAd  extends AdBaseLoad implements AdLoad {
    private volatile static PGloadInteractionExpressAd mInstance;


    /**
     * 单例模式
     *
     * @return
     */
    public static PGloadInteractionExpressAd getInstance() {
        if (mInstance == null) {
            synchronized (PGloadInteractionExpressAd.class) {
                if (mInstance == null) {
                    mInstance = new PGloadInteractionExpressAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(final Activity activity, FrameLayout frameLayout) {
        if (AdCode.getLoadInterstitialId().equals("")&&PGAdId==null) {
            RxLogUtils.e("还未设置ID");
            return;
        }
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        //  TTAdManagerHolder.get().requestPermissionIfNecessary(context);
        //step3:创建TTAdNative对象,用于调用广告请求接口
        TTAdNative TTAdNative = TTAdManagerHolder.get().createAdNative(activity);
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        if (ToastCode){
            Toast.makeText(activity,!PGAdId.equals("") ? PGAdId: AdCode.getLoadInterstitialId(), Toast.LENGTH_SHORT).show();
        }
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(!PGAdId.equals("") ? PGAdId: AdCode.getLoadInterstitialId())
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(300, 450) //期望模板广告view的size,单位dp
                .build();
        //step5:请求广告，对请求回调的广告作渲染处理
        TTAdNative.loadInteractionExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.d("AD_onError", "load error : " + code + ", " + message);
            }
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0) {
                    return;
                }
                TTNativeExpressAd  mTTAd = ads.get(0);
                bindAdListener(mTTAd,activity);
//                if (onFullScreenCallback!=null){
//                    onFullScreenCallback.onAdShow(mTTAd);
//                }else {
                    if (mTTAd != null) {
                        mTTAd.render();
                    } else {
                        RxLogUtils.e("没有添加布局");
                    }
 //               }
            }
        });
    }



    private void bindAdListener(final TTNativeExpressAd ad, final Activity activity) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
            @Override
            public void onAdDismiss() {
                Log.d("onAdDismiss","广告关闭");
                if (listener!=null){
                    listener.close();
                }
            }

            @Override
            public void onAdClicked(View view, int type) {
                Log.d("onAdClicked","广告被点击");
                if (listener!=null){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            listener.onAdClicked();
                        }
                    },200);
                }
            }

            @Override
            public void onAdShow(View view, int type) {
                Log.d("onAdShow","广告展示");
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                Log.d("onRenderFail", msg + " code:" + code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp
                Log.d("onRenderSuccess","渲染成功");
                ad.showInteractionExpressAd(activity);

            }
        });
        //  bindDislike(ad, false);
        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
            return;
        }
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
                //TToast.show(InteractionExpressActivity.this, "点击开始下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {

            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                // TToast.show(InteractionExpressActivity.this, "下载暂停，点击继续", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                // TToast.show(InteractionExpressActivity.this, "下载失败，点击重新下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                //TToast.show(InteractionExpressActivity.this, "安装完成，点击图片打开", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                // TToast.show(InteractionExpressActivity.this, "点击安装", Toast.LENGTH_LONG);
            }
        });
    }



}
